package com.hanfb.mall.inventory.controller;

import com.hanfb.mall.inventory.model.ProductInventory;
import com.hanfb.mall.inventory.request.ProductInventoryCacheReloadRequest;
import com.hanfb.mall.inventory.request.ProductInventoryDbUpdateRequest;
import com.hanfb.mall.inventory.request.Request;
import com.hanfb.mall.inventory.service.ProductInventoryService;
import com.hanfb.mall.inventory.service.RequestAsyncProcessService;
import com.hanfb.mall.inventory.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @作者： Hanfb
 * @描述：
 * @时间： 2020年10月12日
 */
@RestController
public class ProductInventoryController {

    @Autowired
    private ProductInventoryService productInventoryService;

    @Autowired
    private RequestAsyncProcessService requestAsyncProcessService;

    /**
     * 更新商品库存
     * @param productInventory
     * @return
     */
    @RequestMapping("/updateProductInventory")
    public Response updateProductInventory(ProductInventory productInventory) {

        System.out.println("===========日志============：接收到商品库存更新请求，商品id=" + productInventory.getProductId() + ",商品库存数量cnt=" + productInventory.getInventoryCnt());

        Response response = null;
        try {
            Request request = new ProductInventoryDbUpdateRequest(productInventory, productInventoryService);
            requestAsyncProcessService.process(request);
            response = new Response(Response.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            response = new Response(Response.FAILURE);
        }
        return response;

    }


    /**
     * 获取商品库存
     * @param productId
     * @return
     */
    @RequestMapping("/getProductInventory")
    public ProductInventory getProductInventory(Integer productId) {
        System.out.println("===========日志============：接收到商品库存读取请求，商品id=" + productId);
        ProductInventory productInventory = null;
        try {
            Request request = new ProductInventoryCacheReloadRequest(
                    productId, productInventoryService, false);
            requestAsyncProcessService.process(request);
            long startTime = System.currentTimeMillis();
           // System.out.println("===========日志============：startTime="+startTime);
            long waitTime = 0L;
            long endTime = 0L;
            while (true) {
                //System.out.println("===========日志============：waitTime="+waitTime);
                //等待超过200ms 跳出
                if (waitTime > 25000) {
                    break;
                }
                productInventory = productInventoryService.getProductInventoryCache(productId);
                if (productInventory != null) {
                    System.out.println("===========日志============：在200ms内读取到了redis中的缓存，商品id=" + productInventory.getProductId() + ",商品库存数量cnt=" + productInventory.getInventoryCnt());
                    return productInventory;
                } else {
                    //如果没有在缓存中读取到 等待20ms 进行下一次的读取
                    Thread.sleep(20);
                    endTime = System.currentTimeMillis();
                    //System.out.println("===========日志============：endTime="+endTime);
                    waitTime = endTime - startTime;
                }
            }

            //尝试从数据库中读取一次数据
            productInventory = productInventoryService.findProductInventory(productId);
            if (productInventory != null) {
                //刷新一个缓存
                // productInventoryService.setProductInventoryCache(productInventory);
                request = new ProductInventoryCacheReloadRequest(
                        productId, productInventoryService, true);
                requestAsyncProcessService.process(request);
                return productInventory;
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return new ProductInventory(productId, -1L);

    }
}
